import time
import hmac
import hashlib
import base64
import urllib.parse
import requests
import json


def send_message(webhooks, secrets, datas):
    timestamp = str(round(time.time() * 1000))
    secret_enc = secrets.encode('utf-8')
    string_to_sign = '{}\n{}'.format(timestamp, secrets)
    string_to_sign_enc = string_to_sign.encode('utf-8')
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
    wh = webhooks + '&timestamp=' + timestamp + '&sign=' + sign
    # 定义数据类型
    headers = {'Content-Type': 'application/json'}
    # 发送post请求
    res = requests.post(wh, data=json.dumps(datas), headers=headers)
    return res.text
